import 'package:aichat_flutter_wallet/module_common/utils/colors.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/image/public_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lib_database/lib_database.dart';
import 'package:lib_util/utils/utils.dart';

// 公用组件
class CommonWidgetUtil {
  static Text textWidget(text, {double fontsize = 12, Color color = AppColors.c_868391}) {
    return Text(
      text,
      style: TextStyle(color: color, fontSize: fontsize),
    );
  }

  // 左右布局的提示
  static Widget contentItemWidget(String key, Widget child) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [textWidget(key, fontsize: 14.sp), child],
    );
  }

  // 带有圆圈的提示
  static Widget tipWidget(
    String text,
  ) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Transform.translate(
          offset: Offset(0, 7),
          child: Container(
            width: 4.w,
            height: 4.w,
            decoration: BoxDecoration(
              color: AppColors.c_868391,
              shape: BoxShape.circle,
            ),
          ),
        ),
        SizedBox(
          width: 3.w,
        ),
        Expanded(child: CommonWidgetUtil.textWidget(text))
      ],
    );
  }

  // 网站信息组件
  static Widget websizeWidget({String image = '', String title = '', String subTitle = ''}) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        image.isNotEmpty
            ? ClipOval(
                child: PublicImage(
                  //dason
                  image: image,
                  width: 46.w,
                  height: 46.w,
                ),
              )
            : Image.asset(
                'assets/images/dapp/default-avatar.png',
                width: 46.w,
                height: 46.w,
              ),
        SizedBox(
          width: 10.w,
        ),
        Expanded(
            child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              title,
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(color: AppColors.c_333333, fontSize: 14.sp),
            ),
            SizedBox(
              height: 3.w,
            ),
            textWidget(subTitle, color: AppColors.c_BEBDCA, fontsize: 12.sp)
          ],
        ))
      ],
    );
  }

  static Widget walletCoinInfoWidget(
      {String image = '',
      String coinName = 'BNB',
      String walletName = 'MPC Wallet',
      Token? token,
      String chainName = ''}) {
    return Row(
      children: [
        image.isNotEmpty
            ? ClipOval(
                child: PublicImage(
                  image: image,
                  width: 40.w,
                  height: 40.w,
                ),
              )
            : Image.asset(
                image.isNotEmpty ? image : 'assets/images/dapp/default-avatar.png',
                width: 40.w,
                height: 40.w,
              ),
        SizedBox(
          width: 10.w,
        ),
        Expanded(
            child: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                textWidget(chainName, color: AppColors.c_333333, fontsize: 14.sp),
                textWidget('Balance', color: AppColors.c_868391, fontsize: 12.sp)
              ],
            ),
            SizedBox(
              height: 2.w,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                textWidget(walletName, color: AppColors.c_333333, fontsize: 12.sp),
                // (\$${Utils.formatStringBalance(
                //     (double.parse(token?.balance ?? '0.00') * double.parse(token?.priceusd ?? '0')).toString(),
                //     fractionDigits: 2,
                //   )})
                textWidget(
                    '''${Utils.formatStringBalance(token?.balance ?? '0.00', fractionDigits: Config.balanceFixedNum, trimDecimalZero: true)} ${coinName}''',
                    color: AppColors.c_868391, fontsize: 12.sp)
              ],
            )
          ],
        ))
      ],
    );
  }

  static Widget tokenInputWidget(
      {bool canInput = false,
      String coinName = '',
      double price = 0,
      String value = '',
      Function? onChange,
      Function? all,
      TextEditingController? editingController}) {
    EdgeInsetsGeometry? padding = canInput
        ? EdgeInsets.only(left: 15.w, right: 15.w, top: 0, bottom: 0)
        : EdgeInsets.only(left: 15.w, right: 15.w, top: 13.w, bottom: 13.w);
    return Container(
      width: double.infinity,
      // top: 13, bottom: 13
      padding: padding,
      decoration: BoxDecoration(color: AppColors.c_F6F6F6, borderRadius: BorderRadius.circular(10)),
      child: Row(children: [
        if (!canInput)
          Expanded(
              child: Container(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                textWidget(value == '' ? '- -' : value, fontsize: 14.sp, color: AppColors.c_333333),
                // textWidget(
                //     '≈\$${Utils.formatStringBalance((double.parse(value == '' ? '0' : value) * price).toString(), fractionDigits: Config.priceNum, trimDecimalZero: true)}')
              ],
            ),
          )),
        if (canInput)
          Expanded(
              child: Container(
            // height: 30,
            child: Transform.translate(
              offset: Offset(0, 0),
              child: TextField(
                controller: editingController,
                onChanged: (value) {
                  onChange!(value);
                },
                keyboardType: TextInputType.number,
                maxLines: 1,
                decoration: InputDecoration(
                    filled: false,
                    labelStyle: TextStyle(color: Colors.red),
                    // fillColor: Colors.transparent,
                    border: InputBorder.none,
                    hintStyle: TextStyle(color: AppColors.c_BEBDCA, fontSize: 14.sp, fontWeight: FontWeight.w400),
                    hintText: 'please input num'),
                inputFormatters: [FilteringTextInputFormatter.digitsOnly],
              ),
            ),
          )),
        SizedBox(
          width: 6,
        ),
        Row(
          children: [
            textWidget(coinName == '' ? '- -' : coinName, fontsize: 14.sp),
            if (canInput)
              Container(
                height: 14,
                width: 1,
                margin: EdgeInsets.symmetric(horizontal: 10.w),
                color: AppColors.c_BEBDCA,
              ),
            // if (!canInput) textWidget('All', fontsize: 14),
            if (canInput)
              InkWell(
                child: textWidget('All', fontsize: 14.sp, color: AppColors.c_6D4DE7),
                onTap: () {
                  if (all != null) {
                    all();
                    onChange!(editingController?.text);
                  }
                },
              )
          ],
        ),
      ]),
    );
  }

  static Widget warnTipWidget() {
    return Container(
      width: double.infinity,
      padding: EdgeInsets.all(12.w),
      decoration: BoxDecoration(color: AppColors.c_FFF8F1, borderRadius: BorderRadius.circular(10.w)),
      child: Row(crossAxisAlignment: CrossAxisAlignment.start, children: [
        Image.asset(
          'assets/images/dapp/warn.png',
          width: 20.w,
          height: 20.w,
        ),
        SizedBox(
          width: 7.w,
        ),
        Expanded(
            child: CommonWidgetUtil.textWidget(
                'The amount you have authorized is infinite. This operation allows the third party to spend all of your token balance until reaching the spending limit or revoking it. Please consider this carefully',
                fontsize: 12.sp,
                color: AppColors.c_FF5100))
      ]),
    );
  }
}
